package com.usermanage.controller;

import com.contract.Bean.ContractBean1;
import com.contract.Bean.ContractChgSmy;
import com.contract.Common.ContractChgSmyIOC;
import com.contract.Common.ContractIOC;
import com.ordercore.Util.ResponseData.ResponseData;
import com.usermanage.SecurityConfig.SpringSecurityUtils;
import com.usermanage.SecurityConfig.UserDetail;
import com.usermanage.beanParam.PermissionBean;
import com.usermanage.beanParam.UserBean;
import com.usermanage.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/contractModifyController")
public class ContractModifyController {

    @RequestMapping("/list.page")
    public ModelAndView listPage() {
        ModelAndView mv = new ModelAndView();
        //获得用户登陆信息
        UserDetail userDetail = SpringSecurityUtils.getCurrentUser();
        //从用户登陆信息中获取用户权限列表
        List<PermissionBean> permissionBeanList = userDetail.getList();
        for (PermissionBean bean : permissionBeanList) {
            if (bean.getPermission_url().equals("contractModifyConfirm.json")) {
                mv.addObject("confirm", true);
            }
            if (bean.getPermission_url().equals("contractModifyRefuse.json")) {
                mv.addObject("refuse", true);
            }
        }
        mv.setViewName("/contractModify/list");
        return mv;
    }

    /**
     * 合同变更列表
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/contractChgSmyList.json", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public ResponseData contractChgSmyList(int page, int limit, String key) {
        String company_id = SpringSecurityUtils.getCurrentUser().getCompany_id();
        if ("1".equals(company_id)) {
            return ContractChgSmyIOC.contractChgSmyList(page, limit, key, null);
        } else {
            return ContractChgSmyIOC.contractChgSmyList(page, limit, key, company_id);
        }
    }

    @RequestMapping(value = "/contractChgSmyUpt.json", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public ResponseData contractChgSmyUpt(Integer id, String stat) {
        return ContractChgSmyIOC.contractChgSmyUpt(id, stat);
    }

    @RequestMapping(value = "/confirm.json", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public ResponseData contractConfirm(Integer id, String stat) {
        return ContractChgSmyIOC.confirm(id, stat);
    }

    @RequestMapping(value = "/contractChgDtlList.json", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public ResponseData contractChgDtlList(int page, int limit, String key, String id) {
        Integer smy_id = Integer.valueOf(id);
        return ContractChgSmyIOC.contractChgDtlList(page, limit, key, smy_id);
    }

    @RequestMapping(value = "/contractChgSmyDtl.json", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public ResponseData contractChgSmyDtl(String id) {
        Integer smyId = Integer.valueOf(id);
        ResponseData responseData = ContractChgSmyIOC.contractChgSmyDtl(smyId);
        ContractChgSmy map = (ContractChgSmy) responseData.getData();
        Integer contract_id = map.getContract_smy_id();
        ResponseData responseData1 = ContractIOC.findById(contract_id);
        ContractBean1 contractBean1 = (ContractBean1) responseData1.getData();
        Map result = StringUtil.objectToMap(map);
        result.put("contract_name", contractBean1.getContract_name());
        result.put("contract_no", contractBean1.getContract_no());
        responseData.setData(result);
        return responseData;
    }

    @RequestMapping("/detail.page")
    public ModelAndView detailPage(String id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("id", id);
        mv.setViewName("/contractModify/detail");
        return mv;
    }
}
